import { getNetworkPageSelect } from '@/api/network'
import { ref } from 'vue'

/**
 * 站点下拉选项列表服务
 */
export default function () {
  const getNetworkOptionListLoading = ref(false)
  const getNetworkOptionListParam = ref<NetworkAPIRequest.GetOptionsList>({
    ascriptionType: '',
    departmentId: '',
    id: '',
    limit: 50,
    name: '',
    page: 1,
  })
  const networkOptionList = ref<Array<NetworkOption>>([])

  const resetGetNetworkOptionListParam = () => {
    getNetworkOptionListParam.value = {
      ascriptionType: '',
      departmentId: '',
      id: '',
      limit: 50,
      name: '',
      page: 1,
    }
  }

  const getNetworkOptionList = async () => {
    try {
      getNetworkOptionListLoading.value = true
      const res = await getNetworkPageSelect(getNetworkOptionListParam.value)
      getNetworkOptionListLoading.value = false
      if (res.data.code === 20000) {
        networkOptionList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getNetworkOptionListLoading.value = false
      return '获取站点下拉选项列表失败，请稍后重试'
    }
  }

  return {
    networkOptionList,
    getNetworkOptionListParam,
    getNetworkOptionListLoading,
    getNetworkOptionList,
    resetGetNetworkOptionListParam,
  }
}
